/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DBHandling.AccountHandling;
import DBHandling.BillHandling;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;

/**
 *
 * @author HuyPV
 */
public class CheckBillsPanel extends javax.swing.JPanel {

    SpinnerDateModel smodelFr;
    SpinnerDateModel smodeTo;
    Vector<String> spNames;
    String to;
    String from;
    Format fm;
    String createdBy;
    boolean ok=false;
    public CheckBillsPanel() {
        initComponents();
        setOption();


    }

    private void setOption() {

        //set option spinner
        smodelFr = new SpinnerDateModel();
        smodeTo = new SpinnerDateModel();
        SpnTo.setModel(smodeTo);
        spnFrom.setModel(smodelFr);
        
        //setDate format
        JComponent editorFr = new JSpinner.DateEditor(spnFrom, "dd MMMM yyyy");
        JComponent editorTo = new JSpinner.DateEditor(SpnTo, "dd MMMM yyyy");
        SpnTo.setEditor(editorTo);
        spnFrom.setEditor(editorFr);


        //load sales person
        spNames = new Vector<String>();
        try {
            spNames = new AccountHandling().getSalesPersonNames();
        } catch (Exception ex) {
            
        }
        cboCreatedBy.removeAllItems();
        cboCreatedBy.addItem("All Sales Person");
        for (String name : spNames) {
            cboCreatedBy.addItem(name);
        }

        //set option date
        fm = new SimpleDateFormat("MM/dd/YYYY");
        Date d1 = smodeTo.getDate();
        to = fm.format(d1);
        lblTo.setText(to);
        Date d2 = smodelFr.getDate();
        from = fm.format(d2);
        lblFrom.setText(from);
    }
    
    private void resetData(){
        lblStatus.setText("");
        lblTotal.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblBanner = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        spnFrom = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        SpnTo = new javax.swing.JSpinner();
        jSeparator1 = new javax.swing.JSeparator();
        btnView = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cboCreatedBy = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblCreatedBy = new javax.swing.JLabel();
        lblFrom = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblTo = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        lblAllBill = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1200, 100));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Check how many bills have been generated", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Handwriting", 0, 18), new java.awt.Color(0, 0, 204))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        spnFrom.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(1344928311782L), null, new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));
        spnFrom.setName("spnFrom"); // NOI18N
        spnFrom.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spnFromStateChanged(evt);
            }
        });
        jPanel1.add(spnFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(159, 60, 183, -1));

        jLabel1.setText("From");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, -1, -1));

        jLabel2.setText("To");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 110, 20, -1));

        SpnTo.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, new java.util.Date(), java.util.Calendar.MONTH));
        SpnTo.setName("SpnTo"); // NOI18N
        SpnTo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                SpnToStateChanged(evt);
            }
        });
        jPanel1.add(SpnTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(159, 110, 183, -1));

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(452, 39, 15, 182));

        btnView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/ok.png"))); // NOI18N
        btnView.setText("View");
        btnView.setName("btnView"); // NOI18N
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });
        jPanel1.add(btnView, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 200, -1, -1));

        jLabel3.setText("Generated by");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, 20));

        cboCreatedBy.setName("cboCreatedBy"); // NOI18N
        cboCreatedBy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCreatedByItemStateChanged(evt);
            }
        });
        jPanel1.add(cboCreatedBy, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 160, 180, -1));

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel4.setText("Total bills :");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 110, -1, 30));

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel5.setText("Created by: ");
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 110, 80, 30));

        lblTotal.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblTotal.setForeground(new java.awt.Color(255, 0, 0));
        lblTotal.setName("lblTotal"); // NOI18N
        jPanel1.add(lblTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 110, 50, 30));

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel6.setText("Status: ");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 240, -1, 20));

        lblStatus.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblStatus.setForeground(new java.awt.Color(0, 0, 204));
        lblStatus.setName("lblStatus"); // NOI18N
        jPanel1.add(lblStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 240, 240, 20));

        jLabel7.setFont(new java.awt.Font("Lucida Handwriting", 0, 24));
        jLabel7.setForeground(new java.awt.Color(0, 0, 204));
        jLabel7.setText("Statistic");
        jLabel7.setName("jLabel7"); // NOI18N
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(580, 20, -1, -1));

        lblCreatedBy.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblCreatedBy.setForeground(new java.awt.Color(255, 0, 0));
        lblCreatedBy.setName("lblCreatedBy"); // NOI18N
        jPanel1.add(lblCreatedBy, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 110, 160, 30));

        lblFrom.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblFrom.setForeground(new java.awt.Color(153, 0, 153));
        lblFrom.setName("lblFrom"); // NOI18N
        jPanel1.add(lblFrom, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 60, 110, 20));

        jLabel11.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel11.setText("From");
        jLabel11.setName("jLabel11"); // NOI18N
        jPanel1.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 60, -1, 20));

        lblTo.setFont(new java.awt.Font("Times New Roman", 1, 14));
        lblTo.setForeground(new java.awt.Color(153, 0, 153));
        lblTo.setName("lblTo"); // NOI18N
        jPanel1.add(lblTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 60, 130, 20));

        jLabel13.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel13.setText("To");
        jLabel13.setName("jLabel13"); // NOI18N
        jPanel1.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 60, 20, 20));

        jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel10.setText("Total bill (all day, all sales person):");
        jLabel10.setName("jLabel10"); // NOI18N
        jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 180, -1, 20));

        lblAllBill.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        lblAllBill.setForeground(new java.awt.Color(102, 0, 102));
        lblAllBill.setName("lblAllBill"); // NOI18N
        jPanel1.add(lblAllBill, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 180, 150, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 150, 870, 270));
    }// </editor-fold>//GEN-END:initComponents

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        try {
            // TODO add your handling code here:
            BillHandling bh = new BillHandling();
            int all = 0;
            if(!ok){
                all = bh.getTotalBill(null, null, "All Sales Person");
                ok = true;
                lblAllBill.setText(String.valueOf(all));
            }
            int total = bh.getTotalBill(from, to, createdBy);
            lblTotal.setText(String.valueOf(total));
            lblCreatedBy.setText(createdBy);
            lblStatus.setText("Completed!");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_btnViewActionPerformed

    private void spnFromStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spnFromStateChanged
        // TODO add your handling code here:
        resetData();
        Date d2 = smodelFr.getDate();
        from = fm.format(d2);
        lblFrom.setText(from);
    }//GEN-LAST:event_spnFromStateChanged

    private void SpnToStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SpnToStateChanged
        // TODO add your handling code here:
        resetData();
        Date d1 = smodeTo.getDate();
        to = fm.format(d1);
        lblTo.setText(to);
    }//GEN-LAST:event_SpnToStateChanged

    private void cboCreatedByItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCreatedByItemStateChanged
        // TODO add your handling code here:
        resetData();
        createdBy = cboCreatedBy.getSelectedItem().toString();
        lblCreatedBy.setText(createdBy);
    }//GEN-LAST:event_cboCreatedByItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner SpnTo;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cboCreatedBy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblAllBill;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblCreatedBy;
    private javax.swing.JLabel lblFrom;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTo;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JSpinner spnFrom;
    // End of variables declaration//GEN-END:variables
}
